﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tyshka.Subroutines.Kernel.Extensions
{
    public static class StringExtension
    {
        public static byte[] ToByteArray(this String s)
        {
            return Encoding.UTF8.GetBytes(s);
        }

        /// <summary>
        /// Returns substring of the first length characters of the string
        /// </summary>
        public static String Left(this String s, int length)
        {
            length = Math.Max(length, 0);
            if (length < s.Length)
            {
                return s.Substring(0, length);
            }
            return s;
        }

        public static String Left(this String s, String till)
        {
            int position = s.IndexOf(till);
            if (position == -1)
            {
                return s;
            }
            return s.Substring(position);
        }

        /// <summary>
        /// Returns substring of the last length characters of the string
        /// </summary>
        public static String Right(this String s, int length)
        {
            length = Math.Max(length, 0);
            if (length < s.Length)
            {
                return s.Substring(s.Length - length, length);
            }
            return s;
        }

        public static String[] Split(this String s, String delimeter)
        {
            return s.Split(new[] { s }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static Dictionary<String, String> ToDictionary(this String s, String outerDelimiter, String innerDelimiter)
        {
            var keyValuePairs = s.Split(outerDelimiter);
            var resultCollection = new Dictionary<String, String>();

            foreach (var keyValueString in keyValuePairs)
            {
                int delimeterPosition = keyValueString.IndexOf(innerDelimiter);
                if (delimeterPosition == -1)
                {
                    continue;
                }
                String keyPart = keyValueString.Substring(0, delimeterPosition);
                String valuePart = keyValueString.Substring(delimeterPosition + 1);
                resultCollection.Add(keyPart, valuePart);
            }

            return resultCollection;
        }

        public static bool ContainsAny(this String s, String characters)
        {
            var symbols = characters.ToCharArray();
            return symbols.Any(symbol => s.Contains(symbol.ToString()));
        }

        public static String RemoveNonNumericCharacters(this String s)
        {
            const String numericSymbols = "1234567890.,";
            var symbols = s.ToCharArray();
            var result = new StringBuilder();
            foreach (var symbol in symbols)
            {
                if (numericSymbols.Contains(symbol.ToString()))
                {
                    result.Append(symbol);
                }
            }
            return result.ToString();
        }

        public static bool EqualsIgnoreCase(this String a, String b)
        {
            return a.Equals(b, StringComparison.InvariantCultureIgnoreCase);
        }

        public static String Merge(this String a, String b)
        {
            return String.Format("{0}{1}", a, b);
        }

        public static String InverseMerge(this String a, String b)
        {
            return String.Format("{0}{1}", b, a);
        }

        public static String Reverse(this String a)
        {
            var symbols = a.ToCharArray();
            Array.Reverse(symbols);
            return new string(symbols);
        }
    }
}
